package lt.vin.currency.server.context;

import java.io.PrintWriter;

import javax.xml.transform.TransformerFactory;
import javax.xml.validation.SchemaFactory;

import lt.vin.currency.server.LoadRatesServlet;
import lt.vin.currency.server.ParserServlet;
import lt.vin.currency.server.dao.CurrencyDao;
import lt.vin.currency.server.dao.impl.CurrencyDaoImpl;
import lt.vin.currency.server.service.CurrencyServiceImpl;

import org.apache.commons.io.output.NullWriter;
import org.w3c.tidy.Tidy;

import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;
import com.google.inject.servlet.ServletModule;

class CurrencyServletModule extends ServletModule {
	@Override
	protected void configureServlets() {
		bind(CurrencyDao.class).to(CurrencyDaoImpl.class);
		bind(XMPPService.class).toInstance(XMPPServiceFactory.getXMPPService());
		
		bind(Tidy.class).toInstance(getTidy());
		bind(TransformerFactory.class).toInstance(TransformerFactory.newInstance());
		bind(SchemaFactory.class).toInstance(SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"));
		
		serve("/lt.vin.currency.Currency/currency").with(CurrencyServiceImpl.class);
		serve("/lt.vin.currency.Admin/currency").with(CurrencyServiceImpl.class);
		serve("/loadRates").with(LoadRatesServlet.class);
		serve("/parse").with(ParserServlet.class);
	}
	
	private Tidy getTidy() {
		Tidy result = new Tidy();
		
		result.setXHTML(true);
		result.setErrout(new PrintWriter(new NullWriter()));
		
		return result;
	}
}